package com.yswy.theater.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.common.convert.Convert;
import com.yswy.common.utils.ListUtils;
import com.yswy.common.utils.StringUtils;
import com.yswy.common.utils.redis.RedisCache;
import com.yswy.common.utils.redis.RedisConstants;
import com.yswy.common.utils.redis.RedisUtil;
import com.yswy.theater.entity.TheaterPlay;
import com.yswy.theater.entity.TheaterPlayGather;
import com.yswy.theater.entity.TheaterPlayUnlock;
import com.yswy.theater.mapper.TheaterPlayUnlockMapper;
import com.yswy.theater.service.TheaterPlayGatherService;
import com.yswy.theater.service.TheaterPlayService;
import com.yswy.theater.service.TheaterPlayUnlockService;
import com.yswy.theater.vo.res.TheaterPlayGatherResVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class TheaterPlayUnlockServiceImpl extends ServiceImpl<TheaterPlayUnlockMapper, TheaterPlayUnlock>
        implements TheaterPlayUnlockService {
    @Autowired
    private TheaterPlayGatherService theaterPlayGatherService ;
    @Autowired
    private TheaterPlayService theaterPlayService ;
    @Autowired
    private RedisCache redisCache;
    @Override
    public void TheaterPlayUnlock(TheaterPlayGather theaterPlayGather , Long userId) {

        save(TheaterPlayUnlock.builder()
                .isUnlock(1)
                .theaterPlayId(theaterPlayGather.getTheaterPlayId())
                .episodes(theaterPlayGather.getEpisodes())
                .createTime(new Date())
                .theaterPlayGatherId(theaterPlayGather.getId())
                .userId(userId)
                .build());
    }

    @Override
    public TheaterPlayGather TheaterPlayUnlockVerify(Long playId, Integer episodes, Long userId) {
        System.out.println(playId +" "+ episodes);
        //判断是否可已解锁
        TheaterPlay theaterPlay =theaterPlayService.getById(playId);
        if (theaterPlay.getUnlockType() == 0){
            TheaterPlayUnlock theaterPlayUnlock = getOne(new LambdaQueryWrapper<TheaterPlayUnlock>()
                    .eq(TheaterPlayUnlock::getTheaterPlayId , playId)
                    .eq(TheaterPlayUnlock::getUserId , userId)
                    .orderByDesc(TheaterPlayUnlock::getEpisodes )
                    .last("limit 1"));
            Integer nowUnlockNum = 1;
            if (theaterPlayUnlock == null){
                nowUnlockNum = theaterPlay.getDefUnlockNum();
            }else {
                if (theaterPlay.getDefUnlockNum() < theaterPlayUnlock.getEpisodes()){
                    nowUnlockNum = theaterPlayUnlock.getEpisodes();
                }else {
                    nowUnlockNum = theaterPlay.getDefUnlockNum();
                }
            }
            if (nowUnlockNum+1 !=episodes ){
                throw new IllegalArgumentException("请先解锁"+(nowUnlockNum+1)+"集");
            }
        }

        TheaterPlayGather theaterPlayGather = theaterPlayGatherService.getOne(new LambdaQueryWrapper<TheaterPlayGather>()
                .eq(TheaterPlayGather::getTheaterPlayId , playId)
                .eq(TheaterPlayGather::getEpisodes ,episodes));
        if (theaterPlayGather ==  null){
            throw new IllegalArgumentException("服务器异常");
        }
        return theaterPlayGather ;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return RedisConstants.SYS_CONFIG_KEY + configKey;
    }


}
